package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Service.MemberService;
import com.itheima.entity.Result;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    //年龄分布
    @RequestMapping("/findByAge")
    public Result findByAge() throws Exception {
        List<Integer> ages = new ArrayList<>();
        //设置计数器，计算各个年龄段的数量
        //0-6为婴幼儿
        int yy =0;
        //7-12为少儿、
        int sn = 0;
        //13-17岁为青少年
        int qsn = 0;
        //18-45岁为青年、
        int qn = 0;
        //46-69岁为中年、
        int zn = 0;
        //>69岁为老年
        int ln = 0;

        //查询所有会员出生日期
        List<String> birthdays=memberService.findByBirthdays();
        //遍历所有日期
        for (String birthday : birthdays) {
            //将String类型转成Date类型
            Date bt1 = DateUtils.parseString2Date(birthday);
            //使用工具类计算出对应出生日期的年龄
            int age = DateUtils.getAge(bt1);
            //判断年龄的范围
            if (age<=6){
                yy+=1;
            }else if(age>=7 && age<=12){
                sn+=1;
            }else if (age>=13 && age<=17){
                qsn+=1;
            }else if (age>=18&&age<=45){
                qn+=1;
            }else if (age>=46&&age<=69){
                zn+=1;
            }else {
                ln+=1;
            }
        }
        Collections.addAll(ages,yy,sn,qsn,qn,zn,ln);


        return new Result(true,"年龄分布查询成功",ages);
    }

}
